package com.jilinxiangyun;

import cn.hutool.core.util.ReflectUtil;
import com.alibaba.jvm.sandbox.api.Information;
import com.alibaba.jvm.sandbox.api.Module;
import com.alibaba.jvm.sandbox.api.ProcessController;
import com.alibaba.jvm.sandbox.api.annotation.Command;
import com.alibaba.jvm.sandbox.api.listener.ext.Advice;
import com.alibaba.jvm.sandbox.api.listener.ext.AdviceListener;
import com.alibaba.jvm.sandbox.api.listener.ext.EventWatchBuilder;
import com.alibaba.jvm.sandbox.api.resource.ModuleEventWatcher;
import org.kohsuke.MetaInfServices;

import javax.annotation.Resource;

/**
 * Description：
 *
 * @author GavinHacker
 * @since Created in 上午10:20 2024/1/9
 */
@MetaInfServices(Module.class)
@Information(id = "sjzd-debug-1")
public class SjzdDebug1 implements Module {

    @Resource
    private ModuleEventWatcher moduleEventWatcher;

    @Command("sjzd-debug-1-cmd")
    public void debug() {

        new EventWatchBuilder(moduleEventWatcher)
                .onAnyClass().hasInterfaceTypes("com.ruoyi.xiangyun.service.IAgentHttpService")
                .onBehavior("orgquery")
                .onWatch(new AdviceListener() {

                    @Override
                    protected void afterReturning(Advice advice) throws Throwable {
                        Object[] slots = advice.getParameterArray();
                        for (Object slot : slots) {
                            System.out.println("View-V:" + slot);
                        }
                        System.out.println("#afterReturning");
                    }

                    @Override
                    protected void before(Advice advice) throws Throwable {
                        Object[] slots = advice.getParameterArray();
                        for (Object slot : slots) {
                            System.out.println("Start-V:" + slot);
                        }
                        try {
                            String v = String.valueOf(ReflectUtil.getFieldValue(slots[1], "parentCode"));
                            System.out.println("OriginValue:" + v);
                            if(v.equals("#")) {
                                ReflectUtil.setFieldValue(slots[1], "parentCode", "000000000000");
                            }
                        }catch (Exception re){
                            System.out.println("ERROR-V:");
                        }
                        for (Object slot : slots) {
                            System.out.println("End-V:" + slot);
                        }
                        System.out.println("before-*");
                        super.before(advice);
                        System.out.println("before-*2");
                    }

                    @Override
                    protected void afterThrowing(Advice advice) throws Throwable {
                        ProcessController.returnImmediately(null);
                    }

                    @Override
                    protected void beforeLine(Advice advice, int lineNum) {
                        System.out.println("beforeLine-@");
                        Object[] slots = advice.getParameterArray();
                        // 遍历局部变量表，获取变量值
                        for (Object slot : slots) {
                            System.out.print("X:" + slot);
                        }
                        super.beforeLine(advice, lineNum);
                        slots = advice.getParameterArray();
                        // 遍历局部变量表，获取变量值
                        for (Object slot : slots) {
                            System.out.print("X:" + slot);
                        }
                        System.out.println("beforeLine-#2");
                    }
                });
    }
}